package com.ctsi.ssdc.web;

import java.net.URI;
import java.net.URISyntaxException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;
import java.lang.Integer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import com.ctsi.ssdc.service.LitemallSystemService;
import com.ctsi.ssdc.domain.LitemallSystem;

import com.ctsi.ssdc.exception.BadRequestAlertException;
import com.ctsi.ssdc.model.PageResult;
import com.ctsi.ssdc.util.HeaderUtil;
import com.ctsi.ssdc.util.ResponseUtil;


/**
 * REST controller for managing LitemallSystem.
 *
 * @author ctsi-biyi-generator
 *
 */
@Api(value = "/api",tags = {"litemall-system-controller"})
@RestController
@RequestMapping("/api")
public class LitemallSystemController {

    private final Logger log = LoggerFactory.getLogger(LitemallSystemController.class);

    private static final String ENTITY_NAME = "litemallSystem";

    private final LitemallSystemService litemallSystemService;

    public LitemallSystemController(LitemallSystemService litemallSystemService) {
        this.litemallSystemService = litemallSystemService;
    }

    @InitBinder   
    public void initBinder(WebDataBinder binder) {   
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");   
        dateFormat.setLenient(true);   
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   
    } 
   
    /**
     * POST  /litemallSystems : Create a new litemallSystem.
     *
     * @param litemallSystem the litemallSystem to create
     * @return the ResponseEntity with status 201 (Created) and with body the new litemallSystem,
        or with status 400 (Bad Request) if the litemallSystem has already an id
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "body",dataType = "LitemallSystem",name = "litemallSystem",value = "the litemallSystem to create")
    })
    @ApiOperation(value = "POST  /litemallSystems : create a new litemallSystem.",
        notes = "POST  /litemallSystems : create a new litemallSystem.",httpMethod = "POST")
    @PostMapping("/litemallSystems")
    public ResponseEntity<LitemallSystem> createLitemallSystem(@RequestBody LitemallSystem litemallSystem) throws URISyntaxException {
        log.debug("REST request to save LitemallSystem : {}", litemallSystem);
        if (litemallSystem.getId() != null) {
            throw new BadRequestAlertException("A new litemallSystem cannot already have an id", ENTITY_NAME, "idexists");
        }

        boolean save =  litemallSystemService.save(litemallSystem);
        Assert.isTrue(save,"Insert failed");
        LitemallSystem result=litemallSystem;

        return ResponseEntity.created(new URI("/api/litemallSystems" + "/" +result.getId() ))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }
	
    /**
     * PUT  /litemallSystems : Updates an existing litemallSystem.
     *
     * @param litemallSystem the litemallSystem to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated litemallSystem,
     * or with status 400 (Bad Request) if the litemallSystem is not valid,
     * or with status 500 (Internal Server Error) if the litemallSystem couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "body",dataType = "LitemallSystem",name = "litemallSystem",value = "the litemallSystem to update")
    })
    @ApiOperation(value = "PUT  /litemallSystems : updates an existing litemallSystem.",
        notes = "PUT  /litemallSystems : updates an existing litemallSystem.",httpMethod = "PUT")
    @PutMapping("/litemallSystems")
    public ResponseEntity<LitemallSystem> updateLitemallSystem(@RequestBody LitemallSystem litemallSystem) throws URISyntaxException {
        log.debug("REST request to update LitemallSystem : {}", litemallSystem);
	    if (litemallSystem.getId() == null) {
	    	return createLitemallSystem(litemallSystem);
	    }

        boolean update = litemallSystemService.updateById(litemallSystem);
        Assert.isTrue(update,"Update failed");
        LitemallSystem result=litemallSystem;

        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * GET  /litemallSystems/:id : get the "id" litemallSystem.
     *
     * @param id the id of the litemallSystem to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the litemallSystem, or with status 404 (Not Found)
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path",dataType = "INTEGER",
        name = "id",value = "the id of the litemallSystem to retrieve")
    })
    @ApiOperation(value = "GET  /litemallSystems/id : get the id litemallSystem.",
        notes = "GET  /litemallSystems/id : get the id litemallSystem.",httpMethod = "GET")
    @GetMapping("/litemallSystems/{id}")
    public ResponseEntity<LitemallSystem> getLitemallSystem(@PathVariable Integer id) {
        log.debug("REST request to get LitemallSystem : {}", id);
        LitemallSystem litemallSystem = litemallSystemService.getById(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(litemallSystem));
    }

    /**
    * GET  /litemallSystems : get the litemallSystems firstStringBaseColumn.
    *
    * @return the ResponseEntity with status 200 (OK) and the list of litemallSystems in body
    */
    @ApiOperation(value = "GET  /litemallSystems/id : get the id litemallSystem.",
        notes = "GET  /litemallSystems/id : get the id litemallSystem.",httpMethod = "GET")
    @GetMapping("/litemallSystems")
    public PageResult<LitemallSystem> getLitemallSystemsList(String keyName ,Pageable pageable) {
        log.debug("REST request to get LitemallSystemsList");
        return litemallSystemService.findFirstStringColumn(keyName ,pageable);
    }
	
    /**
     * DELETE  /litemallSystems/:id : delete the "id" litemallSystem.
     *
     * @param id the id of the litemallSystem to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path",dataType = "INTEGER",name = "id",
        value = "the id of the litemallSystem to delete")
    })
    @ApiOperation(value = "DELETE  /litemallSystems/id : delete the id litemallSystem.",
        notes = "DELETE  /litemallSystems/id : delete the id litemallSystem.",httpMethod = "DELETE")
    @DeleteMapping("/litemallSystems/{id}")
    public ResponseEntity<Void> deleteLitemallSystem(@PathVariable Integer id) {
        log.debug("REST request to delete LitemallSystem : {}", id);
        litemallSystemService.removeById(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
    
}
